package com.sq.util.web;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;

/**
 * @author sq
 *
 */
public class HttpPostUtil extends BaseHttpUtil {

    // 以 params 提交数据 start
    public static String execute(String url, Map<String, String> params) throws Exception {
        return execute(url, params, null);
    }

    public static String execute(String url, Map<String, String> params, Map<String, String> headers) throws Exception {
        return execute(url, params, headers, StandardCharsets.UTF_8);
    }

    public static String execute(String url, Map<String, String> params, Map<String, String> headers, Charset charset)
            throws Exception {
        return execute(getHttpClient(), new HttpPost(url), params, headers, charset);
    }

    public static String execute(CloseableHttpClient httpClient, HttpPost httpPost, Map<String, String> params,
                                 Map<String, String> headers, Charset charset) throws Exception {
        return execute(httpClient, httpPost, getRequestConfig(), params, headers, charset);
    }

    public static String execute(RequestConfig requestConfig, String url, Map<String, String> params) throws Exception {
        return execute(requestConfig, url, params, null);
    }

    public static String execute(RequestConfig requestConfig, String url, Map<String, String> params, Map<String, String> headers) throws Exception {
        return execute(requestConfig, url, params, headers, StandardCharsets.UTF_8);
    }

    public static String execute(RequestConfig requestConfig, String url, Map<String, String> params, Map<String, String> headers, Charset charset)
            throws Exception {
        return execute(getHttpClient(), new HttpPost(url), requestConfig, params, headers, charset);
    }


    private static String execute(CloseableHttpClient httpClient, HttpPost httpPost, RequestConfig requestConfig, Map<String, String> params,
                                  Map<String, String> headers, Charset charset) throws Exception {
        httpPost.setConfig(requestConfig);
        setHeaders(httpPost, headers);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        if (params != null && params.size() > 0) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
        }
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, charset));
        return request(httpClient, httpPost, charset);
    }
    // 以 params 提交数据 end


    // 以 content 提交数据 start
    public static String execute(String url, String content) throws Exception {
        return execute(url, content, null);
    }

    public static String execute(String url, String content, Map<String, String> headers) throws Exception {
        return execute(url, content, headers, StandardCharsets.UTF_8);
    }

    public static String execute(String url, String content, Map<String, String> headers, Charset charset)
            throws Exception {
        return execute(getHttpClient(), new HttpPost(url), content, headers, charset);
    }


    public static String execute(CloseableHttpClient httpClient, HttpPost httpPost, String content,
                                 Map<String, String> headers, Charset charset) throws Exception {
        return execute(httpClient, httpPost, getRequestConfig(), content, headers, charset);
    }

    public static String execute(RequestConfig requestConfig, String url, String content) throws Exception {
        return execute(requestConfig, url, content, null);
    }

    public static String execute(RequestConfig requestConfig, String url, String content, Map<String, String> headers) throws Exception {
        return execute(requestConfig, url, content, headers, StandardCharsets.UTF_8);
    }

    public static String execute(RequestConfig requestConfig, String url, String content, Map<String, String> headers, Charset charset)
            throws Exception {
        return execute(getHttpClient(), new HttpPost(url), requestConfig, content, headers, charset);
    }


    private static String execute(CloseableHttpClient httpClient, HttpPost httpPost, RequestConfig requestConfig, String content,
                                  Map<String, String> headers, Charset charset) throws Exception {
        httpPost.setConfig(requestConfig);
        setHeaders(httpPost, headers);
        httpPost.setEntity(new StringEntity(content, charset));
        return request(httpClient, httpPost, charset);
    }
    // 以 content 提交数据 end
}
